package me.zhengjie.pm.taskassignment.service.impl;

import me.zhengjie.pm.taskassignment.domain.StaffWorkTask;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.taskassignment.repository.StaffWorkTaskRepository;
import me.zhengjie.pm.taskassignment.service.StaffWorkTaskService;
import me.zhengjie.pm.taskassignment.service.dto.StaffWorkTaskDto;
import me.zhengjie.pm.taskassignment.service.dto.StaffWorkTaskQueryCriteria;
import me.zhengjie.pm.taskassignment.service.mapper.StaffWorkTaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "staffWorkTask")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class StaffWorkTaskServiceImpl implements StaffWorkTaskService {

    private final StaffWorkTaskRepository staffWorkTaskRepository;

    private final StaffWorkTaskMapper staffWorkTaskMapper;

    public StaffWorkTaskServiceImpl(StaffWorkTaskRepository staffWorkTaskRepository, StaffWorkTaskMapper staffWorkTaskMapper) {
        this.staffWorkTaskRepository = staffWorkTaskRepository;
        this.staffWorkTaskMapper = staffWorkTaskMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(StaffWorkTaskQueryCriteria criteria, Pageable pageable){
        Page<StaffWorkTask> page = staffWorkTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(staffWorkTaskMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<StaffWorkTaskDto> queryAll(StaffWorkTaskQueryCriteria criteria){
        return staffWorkTaskMapper.toDto(staffWorkTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public StaffWorkTaskDto findById(Integer id) {
        StaffWorkTask staffWorkTask = staffWorkTaskRepository.findById(id).orElseGet(StaffWorkTask::new);
        ValidationUtil.isNull(staffWorkTask.getId(),"StaffWorkTask","id",id);
        return staffWorkTaskMapper.toDto(staffWorkTask);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public StaffWorkTaskDto create(StaffWorkTask resources) {
        return staffWorkTaskMapper.toDto(staffWorkTaskRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(StaffWorkTask resources) {
        StaffWorkTask staffWorkTask = staffWorkTaskRepository.findById(resources.getId()).orElseGet(StaffWorkTask::new);
        ValidationUtil.isNull( staffWorkTask.getId(),"StaffWorkTask","id",resources.getId());
        staffWorkTask.copy(resources);
        staffWorkTaskRepository.save(staffWorkTask);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            staffWorkTaskRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<StaffWorkTaskDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StaffWorkTaskDto staffWorkTask : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("工作分配人", staffWorkTask.getAssignId());
            map.put("工作执行人", staffWorkTask.getWorkerId());
            map.put("任务ID", staffWorkTask.getTaskId());
            map.put("问题ID", staffWorkTask.getIssueId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}